package com.bootdo.common.utils.orm;


import com.bootdo.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *
 * User: Allard
 * Date: 2017/3/31
 * Time: 14:34
 * Email: allards@synnex.com
 */
public class SqlParamAppendUtil extends ArrayList<String> {
    private List<Object> params=new ArrayList<Object>();
    StringBuffer sqlBuffer=new StringBuffer();
    private String[] orderbys;
    public SqlParamAppendUtil(){}
    public SqlParamAppendUtil(String sql){
        sqlBuffer.append(sql);
    }
    public SqlParamAppendUtil(String sql, String[] orderbys){
        sqlBuffer.append(sql);
        this.orderbys =orderbys;
    }

    public SqlParamAppendUtil setOrderbys(String...orderbys){
        this.orderbys=orderbys;
        return this;
    }

    public SqlParamAppendUtil add(String sql,Object param,boolean isLike){
        if(null!=param){
            if(param instanceof String){
                if(!StringUtils.isEmpty(param.toString())){
                    this.add(sql);
                    if(isLike){
                        params.add("%"+((String) param).trim().toUpperCase()+"%");
                    }else{
                        params.add(((String) param).trim());
                    }
                }
            }else{
                this.add(sql);
                params.add(param);
            }
        }
        return this;
    }
    public SqlParamAppendUtil add(String sql,Object param){
        if(null!=param){
            if(param instanceof String){
                if(!StringUtils.isEmpty(param.toString())){
                    this.add(sql);
                    params.add(((String) param).trim());
                }
            }else{
                this.add(sql);
                params.add(param);
            }
        }
        return this;
    }

    public String join(){
        StringBuffer resultSql=new StringBuffer(sqlBuffer);
        Iterator<String> iterator=this.iterator();
        while(iterator.hasNext()){
            resultSql.append(" "+iterator.next());
        }
        if (orderbys != null && orderbys.length > 0) {
            Integer orderBysLength = orderbys.length;
            resultSql.append(" ORDER  BY ");
            for (int i = 0; i < orderBysLength; i++) {
                if (i == orderBysLength - 1) {
                    resultSql.append(" " + orderbys[i]);
                } else {
                    resultSql.append(" " + orderbys[i]+" , ");
                }
            }
        }
        return resultSql.toString();
    }

    public Object[] getParams(){
        return  params.toArray();
    }

    public Integer getParamSize(){
        return params.size();
    }

    public static void main(String[] args){
        String sql1=" and name=? ";
        String param1="xiaohong";

        String sql2=" and old >= ? ";
        Integer param2=25;

        String sql3=" and dep like ?  ";
        String param3="hr";

        SqlParamAppendUtil spl=new SqlParamAppendUtil("select * from table where 1=1 ");
        spl.add(sql1,param1,false).add(sql2,param2,false).add(sql3,param3,true).setOrderbys("name asc","old desc");

        System.out.println(spl.join());
        System.out.println(Arrays.toString(spl.getParams()));

    }


}
